from collections.abc import Iterable, Iterator


# 判断一个类是否为迭代器
class BothIterAndNext:
    def __iter__(self):
        pass

    def __next__(self):
        pass


class OnlyNext:
    def __next__(self):
        pass


class OnlyIter:
    def __iter__(self):
        pass


def invoke_check():
    print("BothIterAndNext")
    print(isinstance(BothIterAndNext(), Iterable))
    print(isinstance(BothIterAndNext(), Iterator))
    print("OnlyNext")
    print(isinstance(OnlyNext(), Iterable))
    print(isinstance(OnlyNext, Iterator))
    print("OnlyIter")
    print(isinstance(OnlyIter, Iterable))
    print(isinstance(OnlyIter, Iterator))


class Reverse:
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]


def invoke_reverse():
    rev = Reverse("hello,world")
    print(next(rev))
    print(next(rev))













